#!/bin/sh

set -x
# Find the base dir
base_dir="$(dirname $0)/.."
if [ -z "${HTTP_BASE}" ]; then
    HTTP_BASE=http://artifacts.ci.centos.org/fedora-atomic
fi

rm -rf logs
mkdir -p logs

currentdir=$(pwd)

# Create directory for temporary files
tmpdir=$(mktemp -d)

# Kill backgrounded jobs on exit
function clean_up {
    ansible-playbook -i hosts ci-pipeline/playbooks/setup-libvirt-image.yml -l kernel_install_slave_baremetal -e 'state=absent'
    JOBS=$(jobs -p)
    if [ -n "${JOBS}" ]; then
      kill ${JOBS}
    fi
    sudo rm -rf $tmpdir
}
trap clean_up EXIT SIGHUP SIGINT SIGTERM

# Using base Fedora 25 qcow to start with
IMG_URL="http://mirror.sfo12.us.leaseweb.net/fedora/linux/releases/26/CloudImages/x86_64/images/Fedora-Cloud-Base-26-1.5.x86_64.qcow2"

if ! [ -f ~/.ssh/id_rsa ]; then
    ssh-keygen -t rsa -f ~/.ssh/id_rsa -N ''
    cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
fi

pubkey=$(cat ~/.ssh/id_rsa.pub)

mkdir -p host_vars
cat << 'EOF' > host_vars/kernel_install_slave_baremetal.yml
qemu_img_path: /var/lib/libvirt/images
bridge: virbr0
libvirt_systems:
 fedora-kernel-host:
   admin_passwd: $5$uX5x24soDWv3G2TH$BYxhEq4HmxjKmyChV0.VTpqxfhqMaRk8LCr34KOg2C7
   memory: 3072
   disk: 10G
EOF
# Cannot be part of last EOF since IMG_URL needs to resolve
cat << EOF >> host_vars/kernel_install_slave_baremetal.yml
   img_url: $IMG_URL
   admin_ssh_rsa: $pubkey
EOF
cat << EOF > hosts
[libvirt-hosts]
kernel_install_slave_baremetal ansible_ssh_host=127.0.0.1 ansible_user=builder become=true
EOF
# Install net-tools
sudo yum -y install net-tools
# Start test VM
ansible-playbook -i hosts ci-pipeline/playbooks/setup-libvirt-image.yml -l kernel_install_slave_baremetal -e 'state=present' -v

PROVISION_STATUS=$?
if [ "$PROVISION_STATUS" != 0 ]; then
    echo "ERROR: Provisioning\nSTATUS: $PROVISION_STATUS"
    exit 1
fi

# Get libvirt IP
IP=$(cat libvirt-hosts | tail -n 1 | cut -d '=' -f 2)

cat << EOF > inventory
[kernel_install_slave]
$IP ansible_user=admin ansible_ssh_pass=admin ansible_become=true ansible_become_pass=admin ansible_python_interpreter=/usr/bin/python3
EOF

BRANCH=${fed_branch}
if [ "${fed_branch}" = "master" ]; then
    BRANCH="rawhide"
fi

ansible-playbook -i inventory ci-pipeline/utils/kernel_install.yml --extra-vars "kernel=${kernel_vr}" --extra-vars "url=${HTTP_BASE}/${BRANCH}/repo/kernel_repo/" -l kernel_install_slave -v > ${currentdir}/logs/kernel-install.out

KERNEL_INSTALL_STATUS=$?
if [ "$KERNEL_INSTALL_STATUS" != 0 ]; then
    echo "ERROR: Upgrading Kernel\nSTATUS: $KERNEL_INSTALL_STATUS"
    exit 1
fi

# Run asavkov script to run ltp suite
rm -rf ktests
git clone https://pagure.io/ktests
pushd ktests/misc/ltp
ansible-playbook -i ../../../inventory test_local.yml -l kernel_install_slave -v > ${currentdir}/logs/ltp-playbook.out
LTP_STATUS=$?
# Copy logs to proper location
sshpass -p admin scp -rp admin@$IP:/mnt/testarea/ltp/logs ${currentdir}/logs/ltp
if [ "$LTP_STATUS" != 0 ]; then
    echo "ERROR: LTP Testing\nSTATUS: $LTP_STATUS"
    exit 1
fi
popd
